package com.jz.netthings.appengine.support;

import com.jz.netthings.appengine.baseData.ProviderData;
import com.jz.netthings.appengine.baseData.ProviderInfo;
import com.jz.netthings.bean.system.JyUser;
import com.jz.netthings.netty.protocol.bean.ZxdcMsgPacket;
import com.jz.netthings.netty.utils.NettyMsgHelper;
import com.jz.netthings.utils.AdscmDateUtils;
import io.netty.channel.ChannelHandlerContext;

import java.util.Base64;
import java.util.Calendar;
import java.util.HashMap;
import java.util.UUID;

/**
 * 组装发送消息
 */
public class SendMsgSupport {


    /**
     * 发送给平板：通知采浆机认证成功
     *
     * @param chairCxt
     */
    public static void revAuthToChair(ChannelHandlerContext chairCxt) {
        ZxdcMsgPacket cmd = new ZxdcMsgPacket();
        cmd.setCmd("zxdc_rev_authentication_donor");
        NettyMsgHelper.sendPacketToChair(chairCxt, cmd);

    }

    /**
     * 发送给平板：给平板的反馈
     *
     * @param chairCxt
     */
    public static void responseToChair(ChannelHandlerContext chairCxt,ZxdcMsgPacket packet,String result) {
        ZxdcMsgPacket cmd = new ZxdcMsgPacket();
        cmd.setSeq(packet.getSeq());
        cmd.setCmd("response");
        cmd.setResponseFromCmd(packet.getCmd());
        HashMap<String, Object> values = new HashMap<String, Object>();
        values.put("result", result);
        cmd.setValues(values);
        NettyMsgHelper.sendPacketToChair(chairCxt, cmd);

    }


    /**
     * 发送给平板：取消浆员
     *
     * @param chairCxt
     */
    public static void cancel_donor(ChannelHandlerContext chairCxt,ProviderData providerData) {
        ZxdcMsgPacket cmd = new ZxdcMsgPacket();
        cmd.setCmd("cancel_donor");
        cmd.setHasResponse(true);
        cmd.setLevel(1);
        HashMap<String, Object> values = new HashMap<String, Object>();
        values.put("donorId", providerData.getProviderNo());
        cmd.setValues(values);
        NettyMsgHelper.sendPacketToChair(chairCxt, cmd);
    }

    /**
     * 发送给平板：加压
     *
     * @param chairCxt
     * @param packet
     */
    public static void startInfating(ChannelHandlerContext chairCxt,ZxdcMsgPacket packet) {
        ZxdcMsgPacket cmd = new ZxdcMsgPacket();
        cmd.setCmd("startInfating");
        cmd.setHasResponse(false);
        cmd.setLevel(2);
        cmd.setResponseFromCmd(packet.getCmd());
        HashMap<String, Object> values = new HashMap<String, Object>();
        values.put("time", packet.getValues().get("infating_start_time"));
        cmd.setValues(values);
        NettyMsgHelper.sendPacketToChair(chairCxt, cmd);
    }

    /**
     * 发送给平板：管压正常
     *
     * @param chairCxt
     */
    public static void pipeNormal(ChannelHandlerContext chairCxt) {
        ZxdcMsgPacket cmd = new ZxdcMsgPacket();
        cmd.setCmd("pipeNormal");
        cmd.setHasResponse(false);
        cmd.setLevel(2);
        NettyMsgHelper.sendPacketToChair(chairCxt, cmd);
    }
    /**
     * 发送给平板：管压低
     *
     * @param chairCxt
     */
    public static void pipeLow(ChannelHandlerContext chairCxt) {
        ZxdcMsgPacket cmd = new ZxdcMsgPacket();
        cmd.setCmd("pipeLow");
        cmd.setHasResponse(false);
        cmd.setLevel(2);
        NettyMsgHelper.sendPacketToChair(chairCxt, cmd);
    }
    /**
     * 发送给平板：结束还输
     *
     * @param chairCxt
     * @param packet
     */
    public static void autotranfusion_end(ChannelHandlerContext chairCxt,ZxdcMsgPacket packet) {
        ZxdcMsgPacket cmd = new ZxdcMsgPacket();
        cmd.setCmd("autotranfusion_end");
        cmd.setHasResponse(false);
        cmd.setLevel(2);
        cmd.setResponseFromCmd(packet.getCmd());
        HashMap<String, Object> values = new HashMap<String, Object>();
        values.put("time", packet.getValues().get("HS_end_time"));
        cmd.setValues(values);
        NettyMsgHelper.sendPacketToChair(chairCxt, cmd);
    }
    /**
     * 发送给平板：开始还输
     *
     * @param chairCxt
     * @param packet
     */
    public static void autotranfusion_start(ChannelHandlerContext chairCxt,ZxdcMsgPacket packet) {
        ZxdcMsgPacket cmd = new ZxdcMsgPacket();
        cmd.setCmd("autotranfusion_start");
        cmd.setHasResponse(false);
        cmd.setLevel(2);
        cmd.setResponseFromCmd(packet.getCmd());
        HashMap<String, Object> values = new HashMap<String, Object>();
        values.put("time", packet.getValues().get("HS_start_time"));
        cmd.setValues(values);
        NettyMsgHelper.sendPacketToChair(chairCxt, cmd);
    }

    /**
     * 发送给平板：结束穿刺
     *
     * @param chairCxt
     * @param packet
     */
    public static void finishPuncture(ChannelHandlerContext chairCxt,ZxdcMsgPacket packet) {
        ZxdcMsgPacket cmd = new ZxdcMsgPacket();
        cmd.setCmd("finishPuncture");
        cmd.setHasResponse(false);
        cmd.setLevel(2);
        cmd.setResponseFromCmd(packet.getCmd());
        HashMap<String, Object> values = new HashMap<String, Object>();
        values.put("time", packet.getValues().get("puncture_end_time"));
        cmd.setValues(values);
        NettyMsgHelper.sendPacketToChair(chairCxt, cmd);
    }
    /**
     * 发送给平板：开始穿刺
     *
     * @param chairCxt
     * @param packet
     */
    public static void startPuncture(ChannelHandlerContext chairCxt,ZxdcMsgPacket packet) {
        ZxdcMsgPacket cmd = new ZxdcMsgPacket();
        cmd.setCmd("startPuncture");
        cmd.setHasResponse(false);
        cmd.setLevel(2);
        cmd.setResponseFromCmd(packet.getCmd());
        HashMap<String, Object> values = new HashMap<String, Object>();
        values.put("time", packet.getValues().get("puncture_start_time"));
        cmd.setValues(values);
        NettyMsgHelper.sendPacketToChair(chairCxt, cmd);
    }
    /**
     * 发送给平板：结束安装耗材
     *
     * @param chairCxt
     * @param packet
     */
    public static void finishInstallCm(ChannelHandlerContext chairCxt,ZxdcMsgPacket packet) {
        ZxdcMsgPacket cmd = new ZxdcMsgPacket();
        cmd.setCmd("finishInstallCm");
        cmd.setHasResponse(false);
        cmd.setLevel(2);
        cmd.setResponseFromCmd(packet.getCmd());
        HashMap<String, Object> values = new HashMap<String, Object>();
        values.put("time", packet.getValues().get("install_cm_end_time"));
        cmd.setValues(values);
        NettyMsgHelper.sendPacketToChair(chairCxt, cmd);
    }
    /**
     * 发送给平板：开始安装耗材
     *
     * @param chairCxt
     * @param packet
     */
    public static void startInstallCm(ChannelHandlerContext chairCxt,ZxdcMsgPacket packet) {
        ZxdcMsgPacket cmd = new ZxdcMsgPacket();
        cmd.setCmd("startInstallCm");
        cmd.setHasResponse(false);
        cmd.setLevel(2);
        cmd.setResponseFromCmd(packet.getCmd());
        HashMap<String, Object> values = new HashMap<String, Object>();
        values.put("time", packet.getValues().get("install_cm_startime"));
        cmd.setValues(values);
        NettyMsgHelper.sendPacketToChair(chairCxt, cmd);
    }
    /**
     * 发送给平板：血浆重量
     *
     * @param chairCxt
     * @param packet
     */
    public static void plasma_weight(ChannelHandlerContext chairCxt,ZxdcMsgPacket packet) {
        ZxdcMsgPacket cmd = new ZxdcMsgPacket();
        cmd.setCmd("plasma_weight");
        cmd.setHasResponse(false);
        cmd.setTimeout(30000);
        cmd.setLevel(2);
        cmd.setResponseFromCmd(packet.getCmd());
        HashMap<String, Object> values = new HashMap<String, Object>();
        values.put("current_weight", packet.getValues().get("XJDZC_weight"));
        cmd.setValues(values);
        NettyMsgHelper.sendPacketToChair(chairCxt, cmd);
    }
    /**
     * 发送给平板：恢复采浆
     *
     * @param chairCxt
     * @param packet
     */
    public static void resumeCollection(ChannelHandlerContext chairCxt,ZxdcMsgPacket packet) {
        ZxdcMsgPacket resume = new ZxdcMsgPacket();
        resume.setCmd("resume");
        resume.setHasResponse(false);
        resume.setLevel(2);
        resume.setResponseFromCmd(packet.getCmd());
        HashMap<String, Object> values = new HashMap<String, Object>();
        values.put("time", packet.getValues().get("pause_plasma_return_time"));
        resume.setValues(values);
        NettyMsgHelper.sendPacketToChair(chairCxt, resume);
    }
    /**
     * 发送给平板：暂停采浆
     *
     * @param chairCxt
     * @param packet
     */
    public static void pauseCollection(ChannelHandlerContext chairCxt,ZxdcMsgPacket packet) {
        ZxdcMsgPacket pause = new ZxdcMsgPacket();
        pause.setCmd("pause");
        pause.setHasResponse(false);
        pause.setLevel(2);
        pause.setResponseFromCmd(packet.getCmd());
        HashMap<String, Object> values = new HashMap<String, Object>();
        values.put("time", packet.getValues().get("pause_plasma_draw_time"));
        pause.setValues(values);
        NettyMsgHelper.sendPacketToChair(chairCxt, pause);
    }
    /**
     * 发送给平板：完成采浆
     *
     * @param chairCxt
     */
    public static void endCollection(ChannelHandlerContext chairCxt) {
        ZxdcMsgPacket end = new ZxdcMsgPacket();
        end.setCmd("end");
        end.setHasResponse(false);
        end.setLevel(2);
//        end.setResponseFromCmd(fromCmd);
        NettyMsgHelper.sendPacketToChair(chairCxt, end);
    }
    /**
     * 发送给平板：开始采浆
     *
     * @param chairCxt
     */
    public static void startCollection(ChannelHandlerContext chairCxt) {
        ZxdcMsgPacket start = new ZxdcMsgPacket();
        start.setCmd("start");
        start.setHasResponse(false);
        start.setLevel(2);
        NettyMsgHelper.sendPacketToChair(chairCxt, start);
    }
    /**
     * 发送给平板：发送浆员信息
     *
     * @param chairCxt
     * @param provider
     * @param providerInfo
     */
    public static void auth_info(ChannelHandlerContext chairCxt, ProviderData provider, ProviderInfo providerInfo) {
        ZxdcMsgPacket alloc_donor = new ZxdcMsgPacket();

        alloc_donor.setCmd("auth_info");
        alloc_donor.setHasResponse(true);
        alloc_donor.setLevel(3);
        HashMap<String, Object> values = new HashMap<String, Object>();
        values.put("regCode", provider.getRegisterNo());
        values.put("donor_id", providerInfo.getCardId());
        values.put("donor_name", providerInfo.getName());
        values.put("sfz_id", providerInfo.getIdNo());
        values.put("age", providerInfo.getAge());
        values.put("sex", providerInfo.getSex());
        values.put("dz",providerInfo.getAddress());
        String photo =  Base64.getEncoder().encodeToString(provider.getPhoto());
        values.put("photo",photo);
        try {
            values.put("name",providerInfo.getName());
            values.put("gender", providerInfo.getSex());
            if (providerInfo.getBirthday() != null) {
                Calendar d = Calendar.getInstance();
                d.setTime(providerInfo.getBirthday());
                int year = d.get(Calendar.YEAR);
                int month = d.get(Calendar.MONTH);
                int day = d.get(Calendar.DAY_OF_MONTH);

                values.put("year", Integer.toString(year));
                values.put("month", Integer.toString(month + 1));
                values.put("day", Integer.toString(day));
            }

            values.put("nationality", "");
            values.put("address",providerInfo.getAddress());

            values.put("face", photo);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        alloc_donor.setValues(values);

        NettyMsgHelper.sendPacketToChair(chairCxt, alloc_donor);
    }
//    /**
//     * 发送给平板：发送浆员信息
//     *
//     * @param chairCxt
//     * @param provider
//     * @param providerInfo
//     */
//    public static void auth_info(ChannelHandlerContext chairCxt, ProviderData provider, ProviderInfo providerInfo) {
//        ZxdcMsgPacket alloc_donor = new ZxdcMsgPacket();
//        alloc_donor.setCmd("auth_info");
//        alloc_donor.setHasResponse(true);
//        alloc_donor.setLevel(3);
//        HashMap<String, Object> values = new HashMap<String, Object>();
//        values.put("regCode", provider.getRegisterNo());
//        SendMsgSupport.setValues(values,provider,providerInfo);
//        alloc_donor.setValues(values);
//
//        NettyMsgHelper.sendPacketToChair(chairCxt, alloc_donor);
//    }

    /**
     * 发送给平板：
     *
     * @param chairCxt
     * @param provider
     * @param providerInfo
     */
    public static void confirm(ChannelHandlerContext chairCxt, ProviderData provider, ProviderInfo providerInfo) {
        ZxdcMsgPacket sendcmd = new ZxdcMsgPacket();
        sendcmd.setCmd("confirm");
        sendcmd.setHasResponse(true);
        sendcmd.setLevel(2);
        HashMap<String, Object> values = new HashMap<String, Object>();
        System.out.println("doConfirmDonor发送给座椅平板" + provider.getIdNo());
        SendMsgSupport.setValues(values,provider,providerInfo);
        sendcmd.setValues(values);
        NettyMsgHelper.sendPacketToChair(chairCxt, sendcmd);
    }


    private static void setValues(HashMap<String, Object> values, ProviderData provider, ProviderInfo providerInfo){
        values.put("donor_id", provider.getProviderNo());
        values.put("sfz_id", provider.getIdNo());
        values.put("donor_name", provider.getName());
        values.put("age", providerInfo.getAge());
        values.put("sex", providerInfo.getSexName());
        values.put("dz", providerInfo.getAddress());
        String photo =  Base64.getEncoder().encodeToString(provider.getPhoto());
        values.put("photo",photo);

        values.put("name", provider.getName());
        values.put("gender", providerInfo.getSexName());
        if (providerInfo.getBirthday() != null) {
            Calendar d = Calendar.getInstance();
            d.setTime(providerInfo.getBirthday());
            int year = d.get(Calendar.YEAR);
            int month = d.get(Calendar.MONTH);
            int day = d.get(Calendar.DAY_OF_MONTH);

            values.put("year", Integer.toString(year));
            values.put("month", Integer.toString(month + 1));
            values.put("day", Integer.toString(day));
        }
        values.put("nationality", "");
        values.put("address", providerInfo.getAddress());
        values.put("face", photo);
    }


    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * 发送给机器：发送护士信息
     *
     * @param ctx
     * @param nurse
     * @param photo
     */
    public static void send_nurse_info(ChannelHandlerContext ctx, JyUser nurse, byte[] photo) {
        ZxdcMsgPacket sendcmd = new ZxdcMsgPacket();
        sendcmd.setSeq(UUID.randomUUID().toString());
        sendcmd.setCmd("send_nurse_info");
        sendcmd.setHasResponse(true);
        sendcmd.setLevel(2);
        HashMap<String, Object> values = new HashMap<String, Object>();
        values.put("nurse_no", nurse.getId());
        values.put("nurse_name", nurse.getName());
        if (nurse.getBirthday() != null) {
            int age = AdscmDateUtils.getAge(nurse.getBirthday());
            values.put("age", age);
        }
        if ("1".equals(nurse.getGender())) {
            values.put("sex", "男");
        } else if ("2".equals(nurse.getGender())) {
            values.put("sex", "女");
        }
        values.put("address", nurse.getAddress());
        String photoStr =  Base64.getEncoder().encodeToString(photo);
        values.put("photo", photoStr);
        sendcmd.setValues(values);

        NettyMsgHelper.sendPacket(ctx, sendcmd);
    }
    /**
     * 发送给机器：发送浆员信息
     *
     * @param ctx
     * @param photo
     * @param providerInfo
     */
    public static void send_plasma_donor_info(ChannelHandlerContext ctx,byte[] photo,ProviderInfo providerInfo) {
        ZxdcMsgPacket sendcmd = new ZxdcMsgPacket();
        sendcmd.setSeq(UUID.randomUUID().toString());
        sendcmd.setCmd("send_plasma_donor_info");
        sendcmd.setHasResponse(true);
        sendcmd.setLevel(2);
        HashMap<String, Object> values = new HashMap<String, Object>();
        values.put("donor_no", providerInfo.getCardId());
        values.put("donor_name", providerInfo.getName());
        values.put("age", providerInfo.getAge());
        values.put("sex", providerInfo.getSexName());
        values.put("address", providerInfo.getAddress());
        String photoStr =  Base64.getEncoder().encodeToString(photo);
        values.put("photo", photoStr);
        sendcmd.setValues(values);
        NettyMsgHelper.sendPacket(ctx, sendcmd);
    }
    /**
     * 发送给机器：认证献浆员
     *
     * @param ctx
     * @param providerInfo
     */
    public static void auth_pass(ChannelHandlerContext ctx,ProviderInfo providerInfo) {
        ZxdcMsgPacket sendcmd = new ZxdcMsgPacket();
        sendcmd.setSeq(UUID.randomUUID().toString());
        sendcmd.setCmd("auth_pass");
        sendcmd.setHasResponse(true);
        sendcmd.setLevel(2);
        HashMap<String, Object> values = new HashMap<String, Object>();
        values.put("donor_no", providerInfo.getCardId());
        sendcmd.setValues(values);
        NettyMsgHelper.sendPacket(ctx, sendcmd);
    }
    /**
     * 发送给机器：认证通过
     *
     * @param ctx
     * @param providerInfo
     */
    public static void auth_result(ChannelHandlerContext ctx,ProviderInfo providerInfo) {
        ZxdcMsgPacket sendcmd = new ZxdcMsgPacket();
        sendcmd.setSeq(UUID.randomUUID().toString());
        sendcmd.setCmd("auth_result");
        sendcmd.setHasResponse(true);
        sendcmd.setLevel(1);
        HashMap<String, Object> values = new HashMap<String, Object>();
        values.put("donorId", providerInfo.getCardId());
        sendcmd.setValues(values);
        NettyMsgHelper.sendPacket(ctx, sendcmd);
    }
    /**
     * 发送给机器：查询浆机状态
     *
     * @param ctx
     */
    public static void queryZxdcStatus(ChannelHandlerContext ctx) {
        ZxdcMsgPacket sendcmd = new ZxdcMsgPacket();
        sendcmd.setSeq(UUID.randomUUID().toString());
        sendcmd.setCmd("query");
        sendcmd.setHasResponse(true);
        sendcmd.setTimeout(20000);
        sendcmd.setLevel(2);
        NettyMsgHelper.sendPacket(ctx, sendcmd);
    }


}
